# -*- coding: utf-8 -*-
"""
@Auth:
@Time: 2022-11-04 10:39
"""
# 数据预处理
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os

import common


def drop_useless_features(data_frame: pd.DataFrame):
    """
    删除无用特征
    :param data_frame: 数据集
    :return: 删除无用特征后的数据集
    """
    return data_frame.drop(common.DROP_FEATURES, axis=1)


def handle_missing(data_frame: pd.DataFrame):
    """
    删除缺失值
    :return: 填充后数据集
    """
    # 将“无”替换为NAN
    data_frame.replace("无", np.nan, inplace=True)
    nan_data = data_frame[data_frame["投保公司流水号"].isna()]
    # 记录缺失值行
    if not os.path.exists(common.MISS_DATA_PATH):
        nan_data.to_csv(common.MISS_DATA_PATH, encoding="utf-8", index=False)
    # 删除缺失值行
    data_frame.dropna(subset=["投保公司流水号"], inplace=True)
    return data_frame


def drop_duplicates(data_frame: pd.DataFrame):
    """
    数据去重
    :return: 没有重复数据的对象
    """
    return data_frame.drop_duplicates()


def change_sort(data_frame: pd.DataFrame):
    """
    将“事故发生详细地址”调整到“理赔金额”的前一列
    :return: 修改后的对象
    """
    return data_frame[common.NEW_SORT_FEATURES]


def draw_frequency_map(series: pd.Series):
    """
    通过频率作图
    :param: series 投保公司流水号 的频率统计
    """
    plt.hist(series, bins=50, density=True, stacked=True)
    plt.show()


def handle_outliers(data_frame: pd.DataFrame):
    """
    通过出现频率来处理异常值
    :return 正常值数据
    """
    col = data_frame["投保公司流水号"].value_counts()
    # print(col)
    find_index = data_frame[data_frame["投保公司流水号"].isin([6389, 6394, 6391])].index.tolist()
    false_data = data_frame[data_frame["投保公司流水号"].isin([6389, 6394, 6391])]

    # 记录异常值行
    if not os.path.exists(common.OUTLIER_DATA_PATH):
        false_data.to_csv(common.OUTLIER_DATA_PATH, encoding="utf-8", index=False)
    # 删除异常值行
    data_frame = data_frame.drop(find_index)

    return data_frame


def do(data_frame: pd.DataFrame):
    """
    数据预处理的所有处理执行
    :return: 预处理过的对象
    """
    # 数据去重
    print("去重前的数据条数：" + str(len(data_frame)))
    data_frame = drop_duplicates(data_frame)
    print("去重后的数据条数：" + str(len(data_frame)))
    # 无改为NAN，统计缺失值行，删除缺失值行
    data_frame = handle_missing(data_frame)
    # 异常值处理
    data_frame = handle_outliers(data_frame)
    # 修改顺序
    data_frame = change_sort(data_frame)
    print("将“事故发生详细地址”调整到“理赔金额”的前一列后的columns：")
    print(data_frame.columns)

    return data_frame
